<template>
	<view class="content">
		<view style="display: flex;align-items: center;justify-content: space-between;color: #666;">
			<view style="background-color: #fff;padding: 20rpx;border-radius: 16rpx;width: 100%;">
				<view>今日运输次数</view>
				<view style="color: #2979ff;font-size: 38rpx;margin-top: 20rpx;">{{num}}</view>
			</view>
			<view style="background-color: #fff;padding: 20rpx;border-radius: 16rpx;margin-left: 30rpx;width: 100%;">
				<view>累计运输量</view>
				<view style="color: #2979ff;font-size: 38rpx;margin-top: 20rpx;">{{countQuantity}} 吨</view>
			</view>
		</view>
		<view style="background-color: #fff;padding: 20rpx;border-radius: 16rpx;width: 100%;margin-top: 20rpx;">
			<view style="display: flex;align-items: center;justify-content: space-between;">
				<view>
					当前状态
				</view>
				<view style="text-align: center;">
					<text v-if="bindCar" style="margin-right: 20rpx;">{{bindCar.plate}}</text>
					<u-tag :text="upperCarStatusText" type="primary" size="mini" />
				</view>
			</view>
			<view style="margin-top: 30rpx;">
				<view style="display: flex;align-items: center;justify-content: space-between;">
					<view style="width: 100%;">
						<u-button type="primary" @click="toStartCar()" :disabled="onOffStatus==0">上车开始</u-button>
					</view>
					<view style="margin-left: 30rpx;width: 100%;">
						<u-button type="info" :disabled="onOffStatus==1" @click="endCar()">下车结束</u-button>
					</view>
				</view>
			</view>
		</view>

		<view v-if="onOffStatus==0"
			style="background-color: #fff;padding: 20rpx;border-radius: 16rpx;width: 100%;margin-top: 20rpx;">
			<view style="width: 100%;">
				<u-button plain type="primary" @click="toCreate()"><u-icon name="plus"></u-icon><text
						style="margin-left: 10rpx;">创建联单</text></u-button>
			</view>
		</view>

		<view style="background-color: #fff;padding: 20rpx;border-radius: 16rpx;width: 100%;margin-top: 20rpx;">
			<u-grid :col="4" :border="false">
				<u-grid-item @click="tosg()">
					<image style="height: 80rpx;width: 80rpx;" src="../../static/index/施工工地@2x(4).png"></image>
					<view class="grid-text">施工工地</view>
				</u-grid-item>
				<u-grid-item @click="toczcg()">
					<image style="height: 80rpx;width: 80rpx;" src="../../static/index/airRmyGl(2).png"></image>
					<view class="grid-text">处置场所</view>
				</u-grid-item>
				<u-grid-item @click="toCar()">
					<image style="height: 80rpx;width: 80rpx;" src="../../static/index/车辆管理.png"></image>
					<view class="grid-text">车辆信息</view>
				</u-grid-item>
				<u-grid-item @click="toysjl()">
					<image style="height: 80rpx;width: 80rpx;" src="../../static/index/运单S(2).png"></image>
					<view class="grid-text">运输记录</view>
				</u-grid-item>
			</u-grid>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				countQuantity: 0,
				num: 0,
				onOffStatus: 1, //上车状态
				upperCarStatusText: '空闲中',
				bindCar: ''
			}
		},
		onLoad() {
			//查询是否上车
			this.selectUpperCar()
		},
		onShow() {
			this.initData()
			let that = this
			uni.$on('upperCar', function(data) {
				if (data.status === 0) {
					that.selectUpperCar()
				}
			})
		},
		methods: {
			initData() {
				this.$u.get("/driver/countData").then(res => {
					if (res.code === 200) {
						this.countQuantity = res.data.countQuantity
						this.num = res.data.num
					}
				})
			},

			selectUpperCar() {
				uni.showLoading({

				})
				this.$u.get("/driver/inCarDetails").then(res => {
					uni.hideLoading()
					if (res.code === 200) {
						if (res.data) {
							this.onOffStatus = res.data.onOffStatus
							let bindCar = res.data
							this.upperCarStatusText = "已上车"
							getApp().globalData.bindCar = bindCar
							this.bindCar = bindCar
						} else {
							this.upperCarStatusText = "空闲中"
							this.onOffStatus = 1
							this.bindCar = ''
							getApp().globalData.bindCar = ''
						}

					}
				})
			},

			endCar() {
				uni.showModal({
					title: '提示',
					content: '确认下车操作',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.$u.post("/driver/lowerrCar", {
								"id": this.bindCar.driverBindVehicleId, //回显（司机上车绑定记录），字段driverBindVehicleId
								"vehicleInfoId": this.bindCar.id, //车辆id
								"driverInfoId": getApp().globalData.driverInfo.id, //司机（驾驶员个人信息id）
								"carCode": this.bindCar.plate, //车牌号
								"transportationCompanyId": this.bindCar.transportationCompanyId, //运输公司id
								"teamId": this.bindCar.teamId, //4G运输公司id
								"transportationCompanyName": this.bindCar.enterpriseName, //运输公司名称
								"onOffStatus": "1", //上下车状态:0.上车 1.下车
								"carUsageStatus": "0" //车辆使用状态：0空闲中 1.使用中2.维修中
							}).then(res => {
								uni.hideLoading()
								if (res.code === 200) {
									this.selectUpperCar()
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},

			toCreate() {
				let that = this
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						uni.navigateTo({
							url: "/pages/order/create/create?areaId=" + res.result
						})
					}
				});
			},

			toStartCar() {
				uni.navigateTo({
					url: "/pages/index/start-car/start-car"
				})
			},

			tosg() {
				uni.navigateTo({
					url: "/pages/index/sgcs/sgcs"
				})
			},

			toczcg() {
				uni.navigateTo({
					url: "/pages/index/czcs/czcs"
				})
			},

			toCar() {
				uni.navigateTo({
					url: "/pages/index/car/car"
				})
			},

			toysjl() {
				uni.navigateTo({
					url: "/pages/index/ysjl/ysjl"
				})
			}

		}
	}
</script>

<style>
	.content {
		padding: 30rpx;
	}

	.grid-text {
		margin-top: 20rpx;
	}
</style>